package com.sjgs.common.utils;

import com.google.common.hash.Hashing;

import java.util.TreeSet;
import java.util.UUID;

public class UUIDUtils {

    public static long nextShortId() {
        UUID uuid = UUID.randomUUID();
        long h = uuid.getMostSignificantBits();
        long l = uuid.getLeastSignificantBits();
        byte[] bytes = new byte[16];
        bytes[0] = (byte) ((h >>> 56) & 0xFF);
        bytes[1] = (byte) ((h >>> 48) & 0xFF);
        bytes[2] = (byte) ((h >>> 40) & 0xFF);
        bytes[3] = (byte) ((h >>> 32) & 0xFF);
        bytes[4] = (byte) ((h >>> 24) & 0xFF);
        bytes[5] = (byte) ((h >>> 16) & 0xFF);
        bytes[6] = (byte) ((h >>> 8) & 0xFF);
        bytes[7] = (byte) (h & 0xFF);
        bytes[8] = (byte) ((l >>> 56) & 0xFF);
        bytes[9] = (byte) ((l >>> 48) & 0xFF);
        bytes[10] = (byte) ((l >>> 40) & 0xFF);
        bytes[11] = (byte) ((l >>> 32) & 0xFF);
        bytes[12] = (byte) ((l >>> 24) & 0xFF);
        bytes[13] = (byte) ((l >>> 16) & 0xFF);
        bytes[14] = (byte) ((l >>> 8) & 0xFF);
        bytes[15] = (byte) (l & 0xFF);
        return Hashing.murmur3_128().hashBytes(bytes).asLong();
    }


    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        //测试千万hash碰撞
        long maxlength = 10;
        long count = 0;
        TreeSet<String> sets = new TreeSet<>();
        TreeSet<String> prefix = new TreeSet<>();
        for (long i = 0; i < maxlength; i++)
            for (int j = 0; j < 20; j++)
                for (long k = 0; k < 100; k++)
                    for (long l = 0; l < 100; l++) {
                        String url = Long.toUnsignedString(nextShortId(), 32) + "/" + j + "/" + k + "/" + l + ".terrain";
                        String hash = Long.toUnsignedString(Hashing.murmur3_128().hashBytes(url.getBytes()).asLong(), 32);
                        System.out.println(hash);
                        sets.add(hash);
                        prefix.add(hash.substring(0, 2));
                        count++;
                    }
        System.out.println(count + ":" + sets.size());
        System.out.println(prefix.size());
        long end = System.currentTimeMillis();
        System.out.println("used time:" + (end - start));

    }
}